﻿using AiMoWindows.Compiler;
using AiMoWindows.Localization;
using AiMoWindows.MainActions.Mode;

namespace AiMoWindows.CodeScripts.Init
{
    class ReadonlyCodeBlocks
    {
        readonly CodeBlock[] readonlyCodeBlocks_CN_RH;
        readonly CodeBlock[] readonlyCodeBlocks_CN_GH;
        readonly CodeBlock[] readonlyCodeBlocks_EN_RH;
        readonly CodeBlock[] readonlyCodeBlocks_EN_GH;
        readonly CodeBlock empty = CodeBlock.Default;
        public ReadonlyCodeBlocks()
        {
            readonlyCodeBlocks_CN_RH = GetCodeBlock(true, true);
            readonlyCodeBlocks_CN_GH = GetCodeBlock(true, false);
            readonlyCodeBlocks_EN_RH = GetCodeBlock(false, true);
            readonlyCodeBlocks_EN_GH = GetCodeBlock(false, false);
        }

        public void GetCurrentCodeBlocks(ScriptComplileMode complileMode, out CodeBlock beforeMain, out CodeBlock beforeFunc, out CodeBlock beforeClass)
        {
            switch (complileMode)
            {
                default:
                case ScriptComplileMode.ExternalAssemblies:
                    {
                        beforeMain = empty;
                        beforeFunc = empty;
                        beforeClass = empty;
                        break;
                    }
                case ScriptComplileMode.RhinoScript:
                    {
                        if (Local.RunningInChinese)
                        {
                            beforeMain = readonlyCodeBlocks_CN_RH[0];
                            beforeFunc = readonlyCodeBlocks_CN_RH[1];
                            beforeClass = readonlyCodeBlocks_CN_RH[2];
                        }
                        else
                        {
                            beforeMain = readonlyCodeBlocks_EN_RH[0];
                            beforeFunc = readonlyCodeBlocks_EN_RH[1];
                            beforeClass = readonlyCodeBlocks_EN_RH[2];
                        }
                        break;
                    }
                case ScriptComplileMode.GrasshopperScript:
                    {
                        if (Local.RunningInChinese)
                        {
                            beforeMain = readonlyCodeBlocks_CN_GH[0];
                            beforeFunc = readonlyCodeBlocks_CN_GH[1];
                            beforeClass = readonlyCodeBlocks_CN_GH[2];
                        }
                        else
                        {
                            beforeMain = readonlyCodeBlocks_EN_GH[0];
                            beforeFunc = readonlyCodeBlocks_EN_GH[1];
                            beforeClass = readonlyCodeBlocks_EN_GH[2];
                        }
                        break;
                    }
            }

        }

        static CodeBlock[] GetCodeBlock(bool inChinese, bool inRH)
        {
            string template = inRH ?
                Utils.FixNewlines(inChinese ? CSharpTemplateCN : CSharpTemplateEN) :
                Utils.FixNewlines(inChinese ? CSharpGHTemplateCN : CSharpGHTemplateEN);
            string[] parts = template.Split(
                separator,
                StringSplitOptions.RemoveEmptyEntries
                );
            CodeBlock[] resultBlocks = parts.Where(x => !string.IsNullOrWhiteSpace(x)).Select(x => new CodeBlock(x, true)).ToArray();
            if (resultBlocks.Length != 3) throw new Exception($"Debug Error: ReadonlyCodeBlocks 无法解析正确的 resultBlocks: [inChinese = {inChinese}], [inRH = {inRH}]");
            return resultBlocks;
        }

        static readonly string CSharpGHTemplateEN =
@"/// <summary>
/// This class is running as a grasshopper component
/// </summary>
public class Script_Instance : GHCSharpScriptInstance
{
    /// <summary>
    /// Click [Run] will expire this component and recalculate
    /// </summary>
    public override void SolveInstance()
    {
//{661C96CD-DE31}
    }//Additional codes of other methods
//{04BE4B3A-3ED4}
}//Additional codes of other class
//{5EFBB44A-673A}
";
        static readonly string CSharpGHTemplateCN =
@"/// <summary>
/// 这个类作为一个Grasshopper电池的Component
/// </summary>
public class Script_Instance : GHCSharpScriptInstance
{
    /// <summary>
    /// 点击[执行]会让Grasshopper重启该电池的运算
    /// </summary>
    public override void SolveInstance()
    {
//{661C96CD-DE31}
    }//接下来可以添加其他函数
//{04BE4B3A-3ED4}
}//接下来可以添加其他类
//{5EFBB44A-673A}
";

        static readonly string CSharpTemplateEN =
@"/// <summary>
/// This class is running as a rhino command 
/// </summary>
public class Script_Instance : CSharpScriptInstance
{
    /// <summary>
    /// Click [Run] will run your code from this Main() function
    /// </summary>
    public override void Main()
    {
//{661C96CD-DE31}
    }//Additional codes of other methods
//{04BE4B3A-3ED4}
}//Additional codes of other class
//{5EFBB44A-673A}
";
        static readonly string CSharpTemplateCN =
@"/// <summary>
/// 这个类作为一个Rhino命令
/// </summary>
public class Script_Instance : CSharpScriptInstance
{
    /// <summary>
    /// 点击[执行]会从这个Main()函数开始运行代码
    /// </summary>
    public override void Main()
    {
//{661C96CD-DE31}
    }//接下来可以添加其他函数
//{04BE4B3A-3ED4}
}//接下来可以添加其他类
//{5EFBB44A-673A}
";
        static readonly string[] separator = new string[]
        {
            //public class Script_Instance : CSharpScriptInstance{
            //public override void Main(){
            "//{661C96CD-DE31}",//main
            //}
            "//{04BE4B3A-3ED4}",//funcAddition
            //}
            "//{5EFBB44A-673A}",//classAddition
        };
    }
}
